import { Toast } from "vant"
import { getCookie, setCookie } from '.././../cookies'
export default {
  namespaced: true,
  state: {
    user: JSON.parse(localStorage.user || '[]'),
    isShow: false
  },
  mutations: {
    LOGIN(state, payload) {
      // console.log(payload.user)
      state.user = payload.user
      // 通过高阶函数some,判断user里是否有密码和账号同时正确的,只要有一个账号满足就成功
      const res = state.user.some(item => {
        return item.username === payload.username && item.password === payload.password
      })
      if (!res) {
        Toast('账号或密码错误')
      } else {
        Toast.loading({
          message: '登陆中...',
          forbidClick: true,
          onClose() {
            location.href = '/home'
          }
        })
        setCookie('user', { username: payload.username, password: payload.password }, 3)
        state.isShow = true
        console.log(getCookie('user'))
      }
    }
  },
  actions: {

  }
}